/**
 * 基于原生websocket， 封装socket方法
 *
 */
import { useUserStore } from '../stores/user';

interface ISocket {
  websocket: any
  connectURL: Array<string>
  socketOpen: boolean
  heartbeatTimer: any
  heartbeatInterval: number
  heartbeatCount: number
  heartbeatCurrent: number
  isReconnect: boolean
  reconnectCount: number
  reconnectCurrent: number
  reconnectTimer: any
  reconnectInterval: number
  init: (receiveMessage: Function | null) => any
  receive: (message: any) => void
  heartbeat: () => void
  send: (data: any) => void
  close: () => void
  reconnect: () => void
}

const socket: ISocket = {
  // websocket 实例
  websocket: null,
  // 连接地址
  connectURL: [],
  // 开启标识
  socketOpen: false,
  // 心跳timer
  heartbeatTimer: null,
  // 心跳发送频率
  heartbeatInterval: 30000,
  // 异常跳动次数
  heartbeatCurrent: 0,
  // 允许最多异常跳动次数
  heartbeatCount: 3,
  // 是否自动重连
  isReconnect: true,
  // 重连次数
  reconnectCount: 5,
  // 已发起重连次数
  reconnectCurrent: 0,
  // 重连timer
  reconnectTimer: null,
  // 重连频率
  reconnectInterval: 5000,

  init: (receiveMessage: Function | null) => {
    // 检查当前浏览器是否支持 WebSocket
    if (!('WebSocket' in window)) {
      console.error('浏览器不支持WebSocket')
      return null
    }

    // 已经创建过连接不再重复创建
    // if (socket.websocket) {
    //   return socket.websocket
    // }

    // 连接地址
    var socketUrl = '10.100.50.50:3001'
    let host = location.host
    var a = host.indexOf(":")
    var l = host.substring(0, a);
    console.log(l);

    if (l == '157.122.132.176') {
      socketUrl = ' 157.122.132.176:3001'
    }
    socket.connectURL = [`ws://${location.host}/api/websocket/connect?Authorization=${useUserStore().token
      }`, `ws://10.100.50.67:3001?token=web`]
    socket.websocket = socket.websocket || new WebSocket(socket.connectURL[0])



    // 解说消息
    socket.websocket.onmessage = (e: MessageEvent) => {
      const { data } = e




      // 心跳检测
      if (data === 'pong') {
        socket.heartbeatCurrent = 0
        return
      }

      if (receiveMessage) {
        receiveMessage(data)
      }
    }

    // 连接成功
    socket.websocket.onopen = (e: Event) => {

      socket.socketOpen = true
      socket.isReconnect = true
      // 开启心跳
      socket.heartbeat()
    }

    // 连接发生错误
    socket.websocket.onerror = (e: Event) => {


    }

    // 关闭连接
    socket.websocket.onclose = (e: CloseEvent) => {


      clearInterval(socket.heartbeatInterval)
      socket.socketOpen = false
      socket.websocket = null

      // 需要重新连接
      socket.reconnect()
    }
  },

  send: data => {
    if (socket.websocket && socket.websocket.readyState === socket.websocket.OPEN) {
      // 开启状态直接发送
      socket.websocket.send(data)
    } else {
      // 重置心跳
      clearInterval(socket.heartbeatTimer)
    }
  },

  receive: (message: any) => {
    let params = JSON.parse(JSON.parse(message.data).data)
    return params
  },

  heartbeat: () => {
    // 重置心跳
    if (socket.heartbeatTimer) {
      clearInterval(socket.heartbeatTimer)
    }
    socket.heartbeatTimer = setInterval(() => {
      // 超过允许最多异常跳动次数视为断线, 重新连接
      if (socket.heartbeatCurrent > socket.heartbeatCount) {
        clearInterval(socket.heartbeatTimer)
        socket.heartbeatCurrent = 0
        socket.reconnect()
      } else {
        socket.send("ping")
        socket.heartbeatCurrent++
      }
    }, socket.heartbeatInterval)
  },

  // 关闭连接
  close: () => {
    clearInterval(socket.heartbeatTimer)
    clearTimeout(socket.reconnectTimer)
    socket.isReconnect = false
    socket.websocket && socket.websocket.close()
  },

  // 重新连接
  reconnect: () => {
    if (socket.isReconnect) {
      socket.reconnectTimer = setTimeout(() => {
        // 超过重连次数
        if (socket.reconnectCurrent >= socket.reconnectCount) {
          clearTimeout(socket.reconnectTimer)
          socket.isReconnect = false
          return
        }
        // 记录重连次数
        socket.reconnectCurrent++

        if (!socket.isReconnect) {
          socket.close()
        } else {

          socket.init(null)
        }
      }, socket.reconnectInterval)
    }
  }
}

export default socket
